package com.movies.service.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movies.service.common.Constants;
import com.movies.service.common.Result;
import com.movies.service.entity.Movies;
import com.movies.service.service.MoviesService;
import com.movies.service.mapper.MoviesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin/movies")
@SaCheckLogin
@SaCheckRole("ADMIN")
public class AdminMoviesController {

    @Autowired
    private MoviesService moviesService; // 仅分页
    @Autowired
    private MoviesMapper moviesMapper;   // 原生 SQL CRUD

//    分页查询电影列表（支持按名称/类型/状态筛选）
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNo,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) String mname,
                       @RequestParam(required = false) String type,
                       @RequestParam(required = false) String status) {
        LambdaQueryWrapper<Movies> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(mname != null && !mname.isEmpty(), Movies::getMname, mname)
                .eq(type != null && !type.isEmpty(), Movies::getType, type)
                .eq(status != null && !status.isEmpty(), Movies::getStatus, status);
        Page<Movies> page = moviesService.page(new Page<>(pageNo, pageSize), wrapper);
        return Result.success("查询成功", page);
    }

//    根据ID获取电影详情
    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        return Result.success(moviesMapper.selectMoviesById(id));
    }

//    新增电影
    @PostMapping
    public Result create(@RequestBody Movies movies) {
        int rows = moviesMapper.insertMovies(movies);
        return rows > 0 ? Result.success("新增成功") : Result.error(Constants.CODE_600, "新增失败");
    }

//    更新电影信息
    @PutMapping
    public Result update(@RequestBody Movies movies) {
        int rows = moviesMapper.updateMoviesById(movies);
        return rows > 0 ? Result.success("更新成功") : Result.error(Constants.CODE_600, "更新失败");
    }

//    删除电影
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        int rows = moviesMapper.deleteMoviesById(id);
        return rows > 0 ? Result.success("删除成功") : Result.error(Constants.CODE_600, "删除失败");
    }

//    批量删除电影
    @DeleteMapping("/batch")
    public Result deleteBatch(@RequestBody java.util.List<Long> ids) {
        int rows = moviesMapper.deleteMoviesByIds(ids);
        return rows > 0 ? Result.success("批量删除成功") : Result.error(Constants.CODE_600, "批量删除失败");
    }
}